<?php

namespace App\Models\Invoice;

use App\Traits\UuidTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class Invoice extends Model
{

    use UuidTrait;

    const TABLE = 'invoices';
    protected $table = self::TABLE;

    const TYPE_ISSUED = 'ISSUED';
    const TYPE_REVERSED = 'REVERSED';

    const STATUS_CREATE = 'CREATE';
    const STATUS_SUCCESS = 'SUCCESS';
    const STATUS_FAILED = 'FAILED';

    protected $casts = [
        'ofd_dat' => 'array',
        'pdf_dat' => 'array',
        'good_details' => 'array',
        'qr_code_dat' => 'array',
        'xml_dat' => 'array'
    ];

    protected $attributes = [
        'ofd_dat' => '[]',
        'pdf_dat' => '[]',
        'good_details' => '[]',
        'qr_code_dat' => '[]',
        'xml_dat' => '[]'
    ];

    public function reversed(): HasOne
    {
        return $this->hasOne(self::class, 'id', 'original_id')->where('type', self::TYPE_REVERSED)->where('status', self::STATUS_SUCCESS);
    }

    public function related(): MorphTo
    {
        return $this->morphTo();
    }
}
